package com.gsm.service.impl;

import com.gsm.dao.SubjectDetailsDao;
import com.gsm.entity.Subject;
import com.gsm.dao.SubjectDao;
import com.gsm.entity.SubjectDetails;
import com.gsm.service.SubjectService;
import com.gsm.entity.Result;
import com.gsm.utils.IdUtils;
import com.gsm.entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 科目(Subject)表服务实现类
 */
@Service
public class SubjectServiceImpl implements SubjectService {
    @Autowired
    private SubjectDao subjectDao;
    @Autowired
    private SubjectDetailsDao subjectDetailsDao;
    @Autowired
    private IdUtils idUtils;


    @Override
    public Result insertSubject(Subject subject) {
        long subjectId = idUtils.nextId();
        int showOrder = 0;
        Subject tmp = subjectDao.selectSubjectMaxShowOrder();
        if (tmp != null) {
            showOrder = tmp.getShowOrder() + 1;
        }
        subject.setSubjectId(subjectId);
        Subject s = Subject.builder()
                .subjectId(subjectId)
                .subjectName(subject.getSubjectName())
                .status(1)
                .showOrder(showOrder)
                .build();
        subjectDao.insertSubject(s);

        //课程详细信息
        long subjectDetailsId = idUtils.nextId();
        SubjectDetails details = subject.getSubjectDetails();
        SubjectDetails subjectDetails = SubjectDetails.builder()
                .subjectDetailsId(subjectDetailsId)
                .subjectId(subjectId)
                .subIntroduction(details.getSubIntroduction())
                .subTeacher(details.getSubTeacher())
                .qqGroup(details.getQqGroup())
                .subPic(details.getSubPic())
                .subTotal(0)
                .subScore(details.getSubScore())
                .status(1)
                .vip(details.getVip())
                .build();
        subjectDetailsDao.insertSubjectDetails(subjectDetails);
        Result result = new Result(true, StatusCode.OK);
        return result;
    }

    /**
     * 根据 主键 subjectId 删除
     *
     * @param subjectId 主键
     * @return
     */
    @Override
    public Result deleteSubjectBySubjectId(Long subjectId) {
        subjectDao.deleteSubjectBySubjectId(subjectId);

        subjectDetailsDao.deleteSubjectDetailsBySubjectId(subjectId);
        Result result = new Result(true, StatusCode.OK);
        return result;
    }

    /**
     * 修改
     *
     * @param subject 实例对象
     * @return
     */
    @Override
    public Result updateSubjectBySubjectId(Subject subject) {
        subjectDao.updateSubjectBySubjectId(subject);
        Result result = new Result(true, StatusCode.OK);
        return result;
    }

    @Override
    public Result updateSubjectAndDetailsBySubjectId(Subject subject) {
        subjectDao.updateSubjectBySubjectId(subject);
        SubjectDetails subjectDetails = subject.getSubjectDetails();
        subjectDetails.setSubjectId(subject.getSubjectId());
        subjectDetailsDao.updateSubjectDetailsBySubjectId(subjectDetails);
        Result result = new Result(true, StatusCode.OK);
        return result;
    }

    @Override
    public Result updateSubjects(List<Subject> subjects) {
        subjectDao.updateSubjects(subjects);
        Result result = new Result(true, StatusCode.OK);
        return result;
    }

    /**
     * 根据 id 查询单条数据
     *
     * @param subjectId 主键
     * @return
     */
    @Override
    public Result selectSubjectBySubjectId(Long subjectId) {
        Subject subject = subjectDao.selectSubjectBySubjectId(subjectId);
        SubjectDetails subjectDetails = subjectDetailsDao.selectSubjectDetailsBySubjectId(subjectId);
        subject.setSubjectDetails(subjectDetails);
        Result result = new Result(true, StatusCode.OK, "", subject);
        return result;
    }

    /**
     * 根据条件查询列表
     *
     * @param subject 实例对象
     * @return
     */
    @Override
    public Result selectSubjectList(Subject subject) {
        List<Subject> list = subjectDao.selectSubjectList(subject);
        list.forEach(i -> {
            SubjectDetails subjectDetails = subjectDetailsDao.selectSubjectDetailsBySubjectId(i.getSubjectId());
            i.setSubjectDetails(subjectDetails);
        });
        Result result = new Result(true, StatusCode.OK, "", list);
        return result;
    }
}